PACKAGE=anybackupserver

deb: dist
	(cd release && rm -rf $(PACKAGE)-$(VERSION))
	(cd release && tar -xzf $(PACKAGE)-$(VERSION).tar.gz)
	#rm release/$(PACKAGE)-$(VERSION)/anybackupserver.spec
	cp -r DEBIAN release/$(PACKAGE)-$(VERSION)
	sed -i 's/^Version:.*/Version: $(VERSION)-$(BUILD_NUMBER)/' release/$(PACKAGE)-$(VERSION)/DEBIAN/control
	sed -i 's/^Architecture:.*/Architecture: $(ARCH)/' release/$(PACKAGE)-$(VERSION)/DEBIAN/control
	(cd release/$(PACKAGE)-$(VERSION) && mv DEBIAN/Makefile .)
	(cd release/$(PACKAGE)-$(VERSION) && make deb && rm Makefile)
	(cd release/$(PACKAGE)-$(VERSION) && find . -type f -exec md5sum {} \; | grep -v 'DEBIAN' > DEBIAN/md5sums)
	sed -i 's@ \./@ @' release/$(PACKAGE)-$(VERSION)/DEBIAN/md5sums
	(cd release/$(PACKAGE)-$(VERSION) && chmod +x DEBIAN/postinst DEBIAN/postrm)
	(cd release/$(PACKAGE)-$(VERSION) && sudo chown -R root:root .)
	(cd release && sudo dpkg-deb --build $(PACKAGE)-$(VERSION) $(PACKAGE)-$(VERSION)-$(BUILD_NUMBER).$(ARCH).deb)
	(cd release && sudo rm -rf $(PACKAGE)-$(VERSION))

dist:
	(mkdir -p release && cd release && mkdir -p $(PACKAGE)-$(VERSION))
	cp server.py release/$(PACKAGE)-$(VERSION)/
	cp -r backupManager release/$(PACKAGE)-$(VERSION)/
	cp upstart.conf release/$(PACKAGE)-$(VERSION)/
	cp initd_script.sh release/$(PACKAGE)-$(VERSION)/
	cp server.cfg release/$(PACKAGE)-$(VERSION)/
	(cd release/ && tar -czvf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION))
	(cd release/ && rm -rf $(PACKAGE)-$(VERSION))
